package com.mf.ergate.controller.sys;

import com.mf.ergate.api.sys.FileStorageInfoRemoteService;
import com.mf.ergate.api.sys.model.dto.FileStorageInfoDeleteDto;
import com.mf.ergate.api.sys.model.dto.FileStorageInfoQueryDto;
import com.mf.ergate.api.sys.model.dto.FileStorageInfoSaveDto;
import com.mf.ergate.api.sys.model.vo.FileStorageInfoVo;
import com.mf.ergate.common.exception.BusinessException;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.sys.FileStorageInfoEntity;
import com.mf.ergate.service.sys.FileStorageInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 文件存储信息Controller
 * User: zhaoming
 * DateTime: 2019-03-11
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class FileStorageInfoController implements FileStorageInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private FileStorageInfoService fileStorageInfoService;

    /**
     * 保存
     * @param fileStorageInfoSaveDto 文件存储信息新增Dto
     * @return 文件存储信息
     */
    public Result<FileStorageInfoVo> save(@RequestBody FileStorageInfoSaveDto fileStorageInfoSaveDto) {
        if(Objects.isNull(fileStorageInfoSaveDto.getFile())){
            throw new BusinessException("文件为必填项");
        }
        FileStorageInfoEntity fileStorageInfoEntity = fileStorageInfoService.save(fileStorageInfoSaveDto);
        FileStorageInfoVo fileStorageInfoVo = MfBeanUtils.copyObject(fileStorageInfoEntity, FileStorageInfoVo.class);
        return new Result<>(fileStorageInfoVo);
    }

    /**
     * 删除(逻辑删除)
     * @param fileStorageInfoDeleteDto 文件存储信息删除Dto
     * @return 执行结果
     */
    public Result<Boolean> delete(@RequestBody FileStorageInfoDeleteDto fileStorageInfoDeleteDto){
        if(Objects.isNull(fileStorageInfoDeleteDto.getUniqueNo())){
            throw new BusinessException("文件存储编号为必填项");
        }
        FileStorageInfoEntity fileStorageInfoEntity = MfBeanUtils.copyObject(fileStorageInfoDeleteDto, FileStorageInfoEntity.class);
        Boolean flag = fileStorageInfoService.delete(fileStorageInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 文件存储信息
     */
    public Result<FileStorageInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        FileStorageInfoEntity fileStorageInfoEntity = fileStorageInfoService.selectByUniqueNo(uniqueNo);
        FileStorageInfoVo fileStorageInfoVo = MfBeanUtils.copyObject(fileStorageInfoEntity, FileStorageInfoVo.class);
        return new Result<>(fileStorageInfoVo);
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 文件存储信息分页列表
     */
    public Result<Page<FileStorageInfoVo>> selectPage(@RequestBody RemotePage<FileStorageInfoQueryDto> remotePage){
        Page<FileStorageInfoEntity> fileStorageInfoEntityPage = fileStorageInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<FileStorageInfoVo> page = MfBeanUtils.copyPage(fileStorageInfoEntityPage, FileStorageInfoVo.class);
        return new Result<>(page);
    }

    /**
     * 下载
     * @param uniqueNo 唯一编号
     * @return 文件
     */
    public Result<byte[]> download(@RequestBody String uniqueNo){
        return new Result<>(fileStorageInfoService.download(uniqueNo));
    }

}